package com.front.managementfront.dictionary.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.front.managementfront.agreement.entity.HktsAgreement;
import com.front.managementfront.dictionary.entity.HktsDictionaryItem;
import com.front.managementfront.dictionary.service.IHktsDictionaryItemService;
import com.front.managementfront.dictionary.vo.DictionaryParamterItemVo;
import com.front.managementfront.fegin.IUserAppServiceClient;
import com.front.managementfront.utils.CustomPageUtils;
import com.front.managementfront.utils.DictionaryConstant;
import com.hkts.management.common.utils.CustomPage;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.common.vo.UserBasicInfoVo;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author yck



 */
@RestController
@RequestMapping("/front/hktsDictionaryItem")
@Api(tags = "字典项接口")
public class HktsDictionaryItemController {
    private final Logger logger = LoggerFactory.getLogger(HktsDictionaryItemController.class);

    @Autowired
    public IHktsDictionaryItemService iHktsDictionaryItemService;
    @Autowired
    private IUserAppServiceClient iUserAppServiceClient;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询数据
     *
     * @param page               分页信息
     * @param hktsDictionaryItem 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHktsDictionaryItemPageList")
    @ApiOperation(value = "字典项分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页行数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "listId", value = "字典id", required = true, paramType = "query", dataType = "string")
     })
    public Result<CustomPage<HktsDictionaryItem>> getHontUserList(@ApiParam(value = "分页",hidden = true) Page<HktsDictionaryItem> page,@ApiParam(value = "字典项内容",hidden = true) HktsDictionaryItem hktsDictionaryItem) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsDictionaryItemService.selectPage(page, hktsDictionaryItem)));
    }

    /**
     * 保存和修改公用的
     *
     * @param hktsDictionaryItem 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsDictionaryItemSave")
    @ApiOperation(value = "字典项新增/修改")
    public Result<Object> hktsDictionaryItemSave(HktsDictionaryItem hktsDictionaryItem) {

        iHktsDictionaryItemService.hktsDictionaryItemSave(hktsDictionaryItem);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsDictionaryItemGetById")
    @ApiOperation(value = "根据id查询")
    public Result<Object> hktsDictionaryItemGetById(String id) {
        HktsDictionaryItem byId = iHktsDictionaryItemService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsDictionaryItemDelete")
    @ApiOperation(value = "根据id删除")
    public Result<Object> hontUserDelete(String id) {
        iHktsDictionaryItemService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsDictionaryItemBatchDelete")
    @ApiOperation(value = "根据ids批量删除")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHktsDictionaryItemService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

    /*
     *
     * 根据listId 查item
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsDictionaryItemsGetByListId")
    @ApiOperation(value = "根据字典id进行查询")
    public Result<Object> hktsDictionaryItemsGetByListId(String listId) {
        List<HktsDictionaryItem> byId = iHktsDictionaryItemService.getByListId(listId);
        return ResultUtil.data(byId);
    }

    /*
     * 杨春科
     * 根据字典key 及字典项名称模糊查询字典项名称
     *将查询字段缓存到redis
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsDictionaryItemsGetByKeyAndItemName")
    @ApiOperation(value = "根据字典key 及字典项名称模糊查询字典项名称--货物类别模糊查询--（货物类别 查找不到则为其他项）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "字典key", dataType = "string",required = true),
            @ApiImplicitParam(name = "itemName", value = "字典项名称", dataType = "string",required = false)

    })
    public Result<Object> hktsDictionaryItemsGetByKeyAndItemName(String key, String itemName) {
        List<HktsDictionaryItem> byId = iHktsDictionaryItemService.hktsDictionaryItemsGetByKeyAndItemName(key, itemName);
      /*
      * 缺少其他项的业务逻辑
      * */

       return ResultUtil.data(byId);
    }
    /*
     * 查询历史搜索数据
     *
     * */

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/historySearchDictionary")
    @ApiOperation(value = "根据字典key进行查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "字典key", dataType = "string",required = true)
    })
    public Result<List<String>> historySearchDictionary(String key) {
        Result<UserBasicInfoVo> userBasicInfoVoResult = iUserAppServiceClient.remoteUserBasicInfo();
        List<String> range = redisTemplate.opsForList().range(DictionaryConstant.DICTIONARY + userBasicInfoVoResult.getResult().getId() + ":" + key, 0, -1);
        return ResultUtil.data(range);
    }

    /*
     * 删除历史搜素记录
     *
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/deleteSearchDictionary")
    @ApiOperation(value = "根据字典key及字典项名称删除 --搜索记录不传则全删")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "字典key", dataType = "string",required = true),
            @ApiImplicitParam(name = "itemName", value = "搜索记录", dataType = "string",required = false)

    })
    public Result<String> deleteSearchDictionary(String key, String itemName) {
        Result<UserBasicInfoVo> userBasicInfoVoResult = iUserAppServiceClient.remoteUserBasicInfo();
        if(StringUtils.isNotBlank(itemName)){
            List<String> range = redisTemplate.opsForList().range(DictionaryConstant.DICTIONARY + userBasicInfoVoResult.getResult().getId() + ":" + key, 0, -1);
            Iterator<String> iterator = range.iterator();
            while (iterator.hasNext()){
                if(iterator.next().equals(itemName)){
                    iterator.remove();
                }
            }
            redisTemplate.delete(DictionaryConstant.DICTIONARY + userBasicInfoVoResult.getResult().getId() + ":" + key);
            redisTemplate.opsForList().rightPushAll(DictionaryConstant.DICTIONARY + userBasicInfoVoResult.getResult().getId() + ":" + key, range);
        }else{
            redisTemplate.delete(DictionaryConstant.DICTIONARY + userBasicInfoVoResult.getResult().getId() + ":" + key);
        }
          return ResultUtil.success("删除成功！");
    }
    /*
    * 根据key查询热门类型
    * */

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getHotCategories")
    @ApiOperation(value = "根据字典key查询热门类别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "字典key", dataType = "string",required = true)
    })
    public Result<List<HktsDictionaryItem>> getHotCategories(String key) {
     List<HktsDictionaryItem>  items= iHktsDictionaryItemService.getHotCategories(key);
    return ResultUtil.data(items);
    }
    /*
    * 根据id修改字典项状态
    * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/updateItemById")
    @ApiOperation(value = "修改字典项名称，单位，禁用，启用 是否为热门")
    public Result<String> updateItemById(DictionaryParamterItemVo vo){
        Result<UserBasicInfoVo> userBasicInfoVoResult = iUserAppServiceClient.remoteUserBasicInfo();
        HktsDictionaryItem byId = iHktsDictionaryItemService.getById(vo.getId());
        iHktsDictionaryItemService.saveOrUpdate(vo.getHktsDictionaryItem(vo, byId,userBasicInfoVoResult.getResult().getId()));
        return ResultUtil.success("修改成功!");
    }
    /** 功能描述:
    *  根据listId 查询类别
    * @return:
    * @Author: 杨春科
    * @Date: 2021-08-06 14:54
    * @Description //TODO
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getHktsDictionaryItemList")
    @ApiOperation(value = "根据listId查询类别,添加货物信息用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "listId", value = "字典id", dataType = "string",required = true)
    })
    public Result<List<HktsDictionaryItem>> getHktsDictionaryItemList(String listId) {
        List<HktsDictionaryItem>  items= iHktsDictionaryItemService.getHktsDictionaryItemList(listId);
        return ResultUtil.data(items);
    }
    /** 功能描述:
    *  根据协议id 求 货物类别
    * @return:
    * @Author: 杨春科
    * @Date: 2021-08-31 16:15
    * @Description //TODO
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getHktsDictionaryByAgreementId")
    @ApiOperation(value = "agreementId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "agreementId", value = "协议id", dataType = "string",required = true)
    })
    public Result<List<HktsDictionaryItem>> getHktsDictionaryByAgreementId(String agreementId) {
        List<HktsDictionaryItem>  items= iHktsDictionaryItemService.getHktsDictionaryByAgreementId(agreementId);
        return ResultUtil.data(items);
    }

    /** 功能描述:
    *根据货物类别查找协议
    * @return:
    * @Author: 杨春科
    * @Date: 2021-08-31 16:45
    * @Description //TODO
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getAgreementContent")
    @ApiOperation(value = "根据listId查询类别,添加货物信息用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cargoTypeId", value = "货物类别id", dataType = "string",required = true),
            @ApiImplicitParam(name = "contractId", value = "合同id", dataType = "string",required = true)
    })
    public Result<HktsAgreement> getAgreementContent(String cargoTypeId, String contractId) {
        return iHktsDictionaryItemService.getAgreementContent(cargoTypeId,contractId);

    }

    /** 功能描述:
    *
    * @return:
    * @Author: 杨春科
    * @Date: 2021-09-01 10:40
    * @Description //TODO
    */

    /** 功能描述:
     *
     * @return:
     * @Author: 杨春科
     * @Date: 2021-09-01 9:03
     * @Description //TODO
     */
    @ApiOperation(value = "根据字典id查询字典项")
    @RequestMapping(value = "/getDictItemByItemId/{itemId}",method = RequestMethod.GET)
    @ResponseBody
    public Result<Map<String,Object>> getDictItemByItemId(@PathVariable String itemId) {

        Map<String,Object> map=iHktsDictionaryItemService.getDictItemByItemId(itemId);

        return ResultUtil.data(map);
    }
}
